<template>
  <div class="login-container">
    <div
      class="form-container"
      :style="formContainerStyle"
    >
      <div style="text-align: center;margin-bottom:15px">
        <img
          src="../../assets/imges/carton_logo.png"
          alt=""
          style="width:160px;height:40px"
        >
      </div>
      <el-tabs
        v-model="activeName"
        style="margin-bottom: 25px"
        @tab-click="handleClick"
      >
        <el-tab-pane
          label="密码登录"
          name="login"
        >
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                id="username"
                ref="username"
                v-model="loginForm.username"
                :placeholder="$t('login.username')"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
                @keyup.enter.native="handleUserName"
              />
            </el-form-item>
            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  id="password"
                  ref="password"
                  v-model="displayPassword"
                  show-password
                  type="password"
                  :placeholder="$t('login.password')"
                  tabindex="2"
                  @keyup.enter.native="handleLogin"
                />
              </el-form-item>
            </el-tooltip>
            <div style="font-size: 13px; margin-bottom: -10px;margin-top: 10px">
              <a
                style="margin-left: 5px;color: blue"
                @click="handleForgetPwdClick"
              >{{ $t('login.forgotPassword') }}</a>&nbsp;&nbsp;
              <span>
                <a style="margin-left: 150px;cursor: auto">{{ $t('login.NoAccount') }}？</a>&nbsp;<a
                  style="color: blue"
                  @click="handleRegister"
                >{{ $t('login.ToRegister') }}</a>
              </span>
            </div>
            <div class="handbutton">
              <el-button
                :loading="loading"
                type="primary"
                round
                style="width:50%;height:47px;"
                @click.native.prevent="handleLogin"
              >
                {{ $t('login.logIn') }}
              </el-button>
              <el-button
                type="primary"
                plain
                class="pause-button"
                style="width:50%;height:47px;"
                @click.native.prevent="handleReset"
              >
                {{ $t('login.reset') }}
              </el-button>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane
          label="验证码登录"
          name="loginByPhone"
        >

        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { validUsername } from '@/utils/validate'
export default {
  name: 'Login',
  components: {

  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!this.password) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'change', validator: validatePassword }]
      },
      activeName: 'login',
      formContainerStyle: {
        // width: window.innerHeight * 0.95 * 1083 / 1850 * 0.8 + 'px',
        width: 460 + 'px',
        marginTop: window.innerHeight * 0.95 * 0.36 + 'px'
      },
      redirect: undefined,
      displayPassword: '',
      loading: false,
      capsTooltip: false,
      registerInfo: false,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
        }
      },
      immediate: true
    },
    displayPassword: function (value) {
      this.password = value
    }
  },
  methods: {
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loginForm.password = this.password
          this.$nextTick(() => {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                localStorage.setItem('username', this.loginForm.username)
                this.$router.push({ path: this.redirect || '/' })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置
    handleReset() {
      this.displayPassword = ''
      this.loginForm.username = ''
      this.loginForm.password = ''
    },
    // 忘记密码
    handleForgetPwdClick() {
      console.log('忘记密码');
    },
    // 去注册
    handleRegister() {
      console.log('去注册');
    },
    handleUserName() {
      this.$refs.password.focus()
    },
    handleClick(tab, event) {
      console.log('tab, event: ', tab, event);
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
  }
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #000000;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container {
  .el-input {
    display: inline-block;
    background: #f7f8f7;
    height: 47px;
    width: 85%;
    input {
      background: #f7f8f7;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #f7f8f7 inset !important;
        -webkit-text-fill-color: black !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #f7f8f7;
    color: #454545;
    margin-bottom: 20px;
    border-radius: 10px;
    /deep/ .el-icon-view {
      font-size: 20px;
    }
  }
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #f49f36;
.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/imges/loginBackground.jpg') no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .login {
    color: $light_gray;
    margin: 0px auto 50px auto;
    text-align: center;
    font-weight: bold;
    border-bottom: 1px #fff solid;
    .title {
      font-size: 30px;
      color: #ffff;
    }
    p {
      margin-top: 0px;
      font-size: 16px;
      color: #d2d2d2;
    }
    img {
      width: 60px;
      height: 60px;
      margin-top: -10px;
    }
  }
  .form-container {
    position: absolute;
    width: 380px !important;
    margin: 200px auto;
    padding-top: 10px;
    margin-left: 70%;
    margin-top: 7% !important;
    .login-form {
      position: relative;
      width: 100%;
      max-width: 100%;
      overflow: hidden;
    }
    /deep/ .el-tabs {
      padding: 20px;
      border-radius: 10px;
      background: #fffeff;
    }
    .registerClass {
      color: red;
      font-size: 12px;
    }
  }
  .handbutton {
    display: flex;
    padding-top: 10px;
    margin-top: 5px;
    margin-bottom: 15px;
    .el-button {
      border-radius: 5px;
    }
  }
  /deep/ .el-tabs__header {
    margin-bottom: 25px;
  }
  /deep/ .el-tabs__nav {
    width: 100% !important;
  }
  /deep/ .el-tabs__item {
    width: 50%;
    font-size: 16px;
    text-align: center;
  }
  /deep/ .el-tabs__active-bar {
    width: 50% !important;
  }
  /deep/ .el-form-item__error {
    margin-top: 5px !important;
  }
}
</style>
